<?php
require_once("../connect.php");
require_once("../../phpmailer/class.phpmailer.php");

class Facade {
    
    function __construct()
    {
        
    }
    
/* Function para executar Querys retornando Array */
    public function executeQuery($value)
    {
        $select = mysql_query($value);
        if($select):
            while($line = mysql_fetch_assoc($select))$var[] = $line;
            return $var;
            mysql_free_result($select);
        else:
            return false;
        endif;
    } 
   
   /* Function para executar Querys sem retornar Array */
    public function execute($value)
    {
        $select = mysql_query($value);

        if($select):
            return true;
            mysql_free_result($select);
        else:
            return false;
        endif;
    }   
    

    
/* CONTATO */    
    
/* seleciona o contato pelo id passado */
    public function getContatoByIndex( $id )
    {
        $sql = "SELECT c.*, t.tc_nome FROM jb_contato c INNER JOIN jb_adm_tipo_contato t ON c.con_FKtipoContato = t.tc_idTipoContato AND c.con_idContatoPK = $id LIMIT 0,1";
        $resultSet = $this->executeQuery( $sql );
        return $resultSet[0];
    }
    
    /* Tipo de Contato */
    public function getTipoContato( $id )
    {
        $sql = "select * from jb_adm_tipo_contato where tc_idTipoContato = $id";
        $resultSet = $this->executeQuery( $sql );
        return $resultSet[0];
    }
    
    
    public function encaminharContato( $emailOrigem, $emailDestino, $texto, $idContato )
    {
        $root = "http://".$_SERVER["SERVER_NAME"];
        
        $contato = $this->getContatoByIndex( $idContato );
       
        $mail = new phpmailer();
        $mail->From = $emailOrigem;
        $mail->FromName = "JBFM - Administração / Contato";

        $mail->isHTML( true );

        $mail->Host = "smtp.adm.infra";
        $mail->Mailer = "smtp";
        $mail->Subject = "FWD:JBFM - " . $contato['tc_nome'];

        // HTML body
        $body = "<html><head>
        <style>
            * {
                font-family:'Trebuchet MS';
                font-size:12px;
            }
        </style>
        </head><body>
            <font face='verdana' size='2'>
            <b>JBFM - Administracao / " . $contato['tc_nome'] . "</b><BR /><BR />
            " . $texto . "<br><br >
            <BR>
                <table width=\"80%\" cellpadding=\"3\" cellspacing=\"3\" align=\"center\">
                <tr>
                    <td colspan='2' align='center'>
                    <a href='$root/Application/?page=ContatoForm&action=2&idPub=" . $idContato . "'>Clique aqui para vizualizar Contato</a>
                    </td>
                </tr>
                <tr style=\"background:#CCCCCC\">
                    <td align=\"right\" style=\"font-weight:bold\">Nome:</td>
                    <td >".$contato["con_nome"]."</td>
                </tr>
                <tr style=\"background:#F1F1F1\" >
                    <td align=\"right\" style=\"font-weight:bold\">E-mail:</td>
                    <td >".$contato["con_email"]."</td>
                </tr>

                <tr style=\"background:#CCCCCC\">
                    <td align=\"right\" style=\"font-weight:bold\">Data do Envio:</td>
                    <td >".$contato["con_data"]."</td>
                </tr>

                <tr style=\"background:#F1F1F1\">
                    <td align=\"right\" style=\"font-weight:bold\">Assunto:</td>
                    <td >".$contato["tc_nome"]."</td>
                </tr>
                <tr style=\"background:#CCCCCC\">
                    <td align=\"right\" style=\"font-weight:bold\">Texto:</td>
                    <td style=\"min-height:100px;\">".$contato["con_texto"]."</td>
                </tr>

                <tr style=\"background:#F1F1F1\">
                    <td align=\"right\" style=\"font-weight:bold\">Resposta:</td>
                    <td >".$contato["con_resposta"]."</td>
                </tr>
                </table>
            </font>
            </body>
            </html>
            ";

        $text_body = $contato['tc_nome'] . "\n\n";

        
        $alt = $this->executeQuery("UPDATE jb_contato SET con_status = 3 WHERE con_idContatoPK = $idContato; ");
        
        
        $mail->Body =  html_entity_decode( $body );
        $mail->AltBody = "";

        $mail->AddAddress( $emailDestino, "JBFM - Administra&ccedil;&atilde;o / Contato" );

        $enviado = $mail->Send();

        $mail->ClearAddresses();
        $mail->ClearAttachments();

        return $enviado;
    }
    
  }
  

  
?>
